//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<aligned_space.h>
///	@path	~/src/lib/memory/
///	@date	2007/11/18
///	@desc	Aligned allocator.

#pragma once

#include "xeres.h"

#include <xmemory>

namespace xeres {

	/*!
		\class	aligned_space
		\brief	Aligned space allocator.
	*/
	template <
		typename _Type ,
		size_t _AlignSize = ALIGN_SIZE
	> class aligned_space : public std::allocator< _Type >
	{
		template< size_t size > struct fix_align
		{
			static const int AlignSize = ALIGN_SIZE;
		};

		template<> struct fix_align <32>
		{
			static const int AlignSize = 32;
		};

	public:

		static const int AlignSize = fix_align< _AlignSize >::AlignSize; 

		//	override some methods

		void deallocate( pointer _Ptr , size_type )
		{
			::_aligned_free( _Ptr );			
		}

		pointer allocate( size_type _Count )
		{
			return ::_aligned_malloc( _Count * sizeof(_Type) , ALIGN_SIZE );
		}
	};

	/*!
		\class	aligned_fixed_space
		\brief	Aligned space allocator with fixed size.
	*/
	template <
		typename _Type ,
		size_t _FixedSize = sizeof(_Type) ,
		size_t _AlignSize = ALIGN_SIZE
	> class aligned_fixed_space : public std::allocator< _Type >
	{
		template< size_t size > struct fix_align
		{
			static const int AlignSize = ALIGN_SIZE;
		};

		template<> struct fix_align <32>
		{
			static const int AlignSize = 32;
		};

	public:

		static const int AlignSize = fix_align< _AlignSize >::AlignSize; 

		//	override some methods

		void deallocate( pointer _Ptr , size_type )
		{
			::_aligned_free( _Ptr );			
		}

		pointer allocate( size_type _Count )
		{
			return ::_aligned_malloc( _Count * _FixedSize , ALIGN_SIZE );
		}
	};

} // namespace
